package com.zyj.myapplication.Activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.zyj.myapplication.Adapter.TieziListAdapter;
import com.zyj.myapplication.Javabean.UserShaitu;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserTieziActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ImageView user_pic, shaitulist_return;
    private String user_photo;
    private String user_name;
    private int user_id;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usertiezi);
        initViews();//初始化
        initEvents();//初始化事件
        showData();
    }


    private void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.user_tiezi_rv);
        shaitulist_return = (ImageView) findViewById(R.id.shaitulist_return);
        context = UserTieziActivity.this;
        user_name = getIntent().getStringExtra("user_name");
        //   user_photo = getIntent().getStringExtra("user_photo");
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id", 0);

    }

    private void initEvents() {
        shaitulist_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void showData() {
        UserModel userModel = new UserModel();
        Call<List<UserShaitu>> call = userModel.getusershaitu(user_id);
        call.enqueue(new Callback<List<UserShaitu>>() {
            @Override
            public void onResponse(Call<List<UserShaitu>> call, Response<List<UserShaitu>> response) {
                List<UserShaitu> mDataList = response.body();
                LinearLayoutManager manager = new LinearLayoutManager(context);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(manager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(new TieziListAdapter(UserTieziActivity.this, mDataList, user_name));
                //  recyclerView.setAdapter(new TieziListAdapter(UserTieziActivity.this, mDataList, user_name, user_photo));
            }

            @Override
            public void onFailure(Call<List<UserShaitu>> call, Throwable t) {
                Toast.makeText(UserTieziActivity.this, "获取列表失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
